package com.telit.firecommand.controller;

import com.telit.common.constant.Constant;
import com.telit.common.dto.DistrictDTO;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.firecommand.service.DistrictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 行政区划
 *
 * @author yuezhuang
 * @date 2022-09-05
 */
@Slf4j
@RestController
@RequestMapping(value = "/district", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "行政区划API", value = "行政区划API", produces = MediaType.APPLICATION_JSON_VALUE)
public class DistrictController {

    @Autowired
    private DistrictService districtService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "districtCode", value = "行政区划编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "rank", value = "行政区划层级, 1-省 2-市 3-区县 4-乡镇 5-村（不传默认为当前下一级）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "current", value = "当前页数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "noPage", value = "是否不分页 true-不分页 默认不分页", defaultValue = "true", paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/qry-page-by-parent-code")
    @ApiOperation(value = "依据上级行政区划编码查询下级行政区划列表（分页）", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<PageData<List<DistrictDTO>>> qryPageByParentCode(
            @RequestParam(value = "districtCode") String districtCode,
            @RequestParam(value = "rank", required = false) Integer rank,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "20") Integer size,
            @RequestParam(value = "noPage", required = false, defaultValue = "true") boolean noPage) {
        return DataReturn.ok(
                districtService.qryListByParentCodeWithPage(districtCode, rank, false, current, size, noPage));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "districtCode", value = "行政区划编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "rank", value = "行政区划层级, 1-省 2-市 3-区县 4-乡镇 5-村（不传默认为当前下一级）", paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/qry-list-by-parent-code")
    @ApiOperation(value = "依据上级行政区划编码查询下级行政区划列表", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<List<DistrictDTO>> qryListByParentCode(
            @RequestParam(value = "districtCode") String districtCode,
            @RequestParam(value = "rank", required = false) Integer rank) {
        return DataReturn.ok(
                districtService.qryListByParentCodeWithPage(districtCode, rank, false, 1, 20, true).getDatas());
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "districtCode", value = "行政区划", required = true, paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/tree-district", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "行政区划树", httpMethod = Constant.REQUEST_METHOD_GET)
    public DataReturn<DistrictDTO> treeDistrict(@RequestParam(value = "districtCode") String districtCode) {
        return DataReturn.ok(districtService.treeDistrict(districtCode));
    }

}
